package procsim;

import java.awt.*;

public class GMMultiplexer extends MMultiplexer implements GraphicalElement {

    private int width = 100;
    private int height = 130;
    private int x, y;
    private boolean rotated = false;

    public GMMultiplexer(int nob, int num) {
        super(nob, num);
        setSize();
    }

    public GMMultiplexer(int nob, int number, MSignal in[], Signal control[]) {
        super(nob, number, in, control);
        setSize();
    }

    public GMMultiplexer(int nob, int number, MSignal in[], MSignal control) {
        super(nob, number, in, control);
        setSize();
    }

    private void setSize(){
        if(inNum == 32) {
            width+=146;
            height+=190;
        }
    }

    @Override
    public void setCoords(int x, int y){
        this.x = x;
        this.y = y;
    }

    @Override
    public int getWidth() {
        return width;
    }

    @Override
    public int getHeight() {
        return height;
    }

    @Override
    public void setSize(int width, int height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public void drawIt(Graphics2D g, int x, int y) {
        Util.saveGraphics(g);
        g.setColor(Color.BLACK);
        g.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
        g.drawRect(x+1, y+1, width-2, height-2);
        if(!rotated){
            g.drawString("E", x+width/2-2, y+14);
            if(inNum==2){
                g.drawString("0", x+5, y+height/2-20);
                g.drawString("1", x+5, y+height/2+30);
                g.drawString("0", x+width-10, y+height/2+5);
                g.drawString("0", x+width/2-2, y+height-6);
            } else if(inNum==4){
                g.drawString("0", x+5, y+height/2-40);
                g.drawString("1", x+5, y+height/2-10);
                g.drawString("2", x+5, y+height/2+20);
                g.drawString("3", x+5, y+height/2+50);
                g.drawString("0", x+width-10, y+height/2+5);
                g.drawString("0", x+width/2-20, y+height-6);
                g.drawString("1", x+width/2+15, y+height-6);
            } else if(inNum==8){
                g.drawString("0", x+5, y+height/2-51);
                g.drawString("1", x+5, y+height/2-35);
                g.drawString("2", x+5, y+height/2-19);
                g.drawString("3", x+5, y+height/2-3);
                g.drawString("4", x+5, y+height/2+13);
                g.drawString("5", x+5, y+height/2+29);
                g.drawString("6", x+5, y+height/2+45);
                g.drawString("7", x+5, y+height/2+61);
                g.drawString("0", x+width-10, y+height/2+5);
                g.drawString("0", x+width/2-22, y+height-6);
                g.drawString("1", x+width/2-2, y+height-6);
                g.drawString("2", x+width/2+18, y+height-6);
            }
            else if(inNum==32){
                g.drawString("0", x+5, y+height/2-148);
                g.drawString("1", x+5, y+height/2-133);
                g.drawString("2", x+5, y+height/2-118);
                g.drawString("3", x+5, y+height/2-103);
                g.drawString("4", x+5, y+height/2-88);
                g.drawString("5", x+5, y+height/2-73);
                g.drawString("6", x+5, y+height/2-58);
                g.drawString("7", x+5, y+height/2-43);
                g.drawString("8", x+5, y+height/2-28);
                g.drawString("9", x+5, y+height/2-13);
                g.drawString("10", x+5, y+height/2+2);
                g.drawString("11", x+5, y+height/2+17);
                g.drawString("12", x+5, y+height/2+32);
                g.drawString("13", x+5, y+height/2+47);
                g.drawString("14", x+5, y+height/2+62);
                g.drawString("15", x+5, y+height/2+77);
                g.drawString("16", x+5, y+height/2+92);
                g.drawString("17", x+5, y+height/2+107);
                g.drawString("18", x+5, y+height/2+122);
                g.drawString("19", x+5, y+height/2+137);
                g.drawString("20", x+5, y+height/2+152);
                g.drawString("0", x+width-205, y+height-5);
                g.drawString("1", x+width-165, y+height-5);
                g.drawString("2", x+width-125, y+height-5);
                g.drawString("3", x+width-85, y+height-5);
                g.drawString("4", x+width-45, y+height-5);
            }
        } else {
            g.drawString("E", x+5, y+height/2+5);
            if(inNum==2){
                g.drawString("0", x+width/2-27, y+14);
                g.drawString("1", x+width/2+23, y+14);
                g.drawString("0", x+width/2-2, y+height-6);
                g.drawString("0", x+width-10, y+height/2+5);
            } else if(inNum==4){
                g.drawString("0", x+width/2-47, y+14);
                g.drawString("1", x+width/2-17, y+14);
                g.drawString("2", x+width/2+13, y+14);
                g.drawString("3", x+width/2+43, y+14);
                g.drawString("0", x+width/2-2, y+height-6);
                g.drawString("0", x+width-10, y+height/2-10);
                g.drawString("1", x+width-10, y+height/2+20);
            } else if(inNum==8){
                g.drawString("0", x+width/2-58, y+14);
                g.drawString("1", x+width/2-42, y+14);
                g.drawString("2", x+width/2-26, y+14);
                g.drawString("3", x+width/2-10, y+14);
                g.drawString("4", x+width/2+6, y+14);
                g.drawString("5", x+width/2+22, y+14);
                g.drawString("6", x+width/2+38, y+14);
                g.drawString("7", x+width/2+54, y+14);
                g.drawString("0", x+width/2-2, y+height-6);
                g.drawString("0", x+width-10, y+height/2-15);
                g.drawString("1", x+width-10, y+height/2+5);
                g.drawString("2", x+width-10, y+height/2+25);
            }
        }
        g.setFont(new Font(Font.MONOSPACED, Font.BOLD, 14));
        Coords nameCoords = getNameCoords();
        g.drawString(name, nameCoords.getX1(), nameCoords.getY1());
        if(enableNotSet){
            Coords e = getEnableCoords(10);
            g.setColor(Color.RED);
            g.drawLine(e.getX1(), e.getY1(), e.getX2(), e.getY2());
        }
        Util.restoreGraphics(g);
    }

    @Override
    public void drawIt(Graphics2D g) {
        drawIt(g, x, y);
    }

    public GMMultiplexer rotate(){
        if(!rotated) {
            rotated = true;
            setSize(130, 100);
        }
        return this;
    }

    public Coords getEnableCoords(int length){
        if(rotated) return new Coords(x-length, y+getHeight()/2, x, y+getHeight()/2);
        else return new Coords(x+getWidth()/2, y-length, x+getWidth()/2, y);
    }

    public Coords getResCoords(int length){
        if(rotated) return new Coords(x+width/2+2, y+height, x+width/2+2, y+height+length);
        else return new Coords(x+width, y+height/2, x+width+length, y+height/2);
    }
    
    private Coords getNameCoords(){
        return new Coords(x+getWidth()/2-1-name.length()*3, y+getHeight()/2+6, 0, 0);
    }

}
